package com.xy.Dao.Impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.xy.Dao.BaseDao;
import com.xy.Dao.NewsDao;
import com.xy.entity.News;

public class NewsDaoImpl extends BaseDao implements NewsDao{

	@Override
	public int getCount() {
		int count=-1;
		String sql="select Count(`nid`) From `news`";
		ResultSet rs=Query(sql, null);
		try {
			if(rs.next()){
				count=rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			CloseAll(rs, null, null);
		}
		
		return count;
	}

	@Override
	public List<News> getPagfeNewsList(int pageNo, int pageSize) {
		List<News> newslist=new ArrayList<News>();
		String sql="SELECT nid,ntid,ntitle,nauthor,ncreateDate,nsummary,tname FROM news,topic WHERE news.ntid=topic.tid ORDER BY ncreateDate DESC LIMIT ?,?";
		Object [] obj={(pageNo-1)*pageSize,pageSize};
		ResultSet rs=Query(sql, obj);
		try {
			while (rs.next()) {
				News news=new News(rs.getInt("nid"), rs.getInt("ntid"), rs.getString("ntitle"), rs.getString("nauthor"), rs.getString("ncreateDate"), rs.getString("nsummary"));
				newslist.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			CloseAll(rs, null, null);
		}
		return newslist;
	}

	@Override
	public List<News> getOne(int Id) {
		List<News> newslist=new ArrayList<News>();
		String sql="SELECT nid,ntid,ntitle,nauthor,nmodifyDate,ncontent,ncreateDate,nsummary,tname FROM news,topic WHERE news.ntid=topic.tid AND nid=?";
		Object [] obj={Id};
		ResultSet rs=Query(sql, obj);
		try {
			while (rs.next()) {
				News news=new News(rs.getInt("nid"), rs.getInt("ntid"), rs.getString("ntitle"), rs.getString("nauthor"), rs.getString("ncreateDate"), rs.getString("nsummary"));
				news.setNmondifyDate("nmodifyDate");
				news.setTname(rs.getString("tname"));
				news.setNcontent(rs.getString("ncontent"));
				newslist.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			CloseAll(rs, null, null);
		}
		return newslist;
	}

	@Override
	public List<News> getAll() {
		List<News> newslist=new ArrayList<News>();
		String sql="SELECT * FROM news";
		Object [] obj={null};
		ResultSet rs=Query(sql, obj);
		try {
			while (rs.next()) {
				News news=new News(rs.getInt("nid"), rs.getInt("ntid"), rs.getString("ntitle"), rs.getString("nauthor"), rs.getString("ncreateDate"), rs.getString("nsummary"));
				newslist.add(news);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			CloseAll(rs, null, null);
		}
		return newslist;
	}
}
